package cn.tedu.service.petcommodity.Impl;

import cn.tedu.commons.response.JsonPage;
import cn.tedu.mapper.pettype.PetTypeMapper;

import cn.tedu.pojo.pet.dto.PetTypeDTO;
import cn.tedu.pojo.pet.entity.PetType;
import cn.tedu.pojo.pet.vo.PetTypeVO;

import cn.tedu.service.petcommodity.IPetTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class PetTypeImpl implements IPetTypeService {
    @Autowired(required = false)
    private PetTypeMapper petTypeMapper;


    @Override
    public void addType(PetTypeDTO petTypeDTO) {
        String typename= petTypeDTO.getTypeName();
        PetType queryResult=petTypeMapper.getByTypeName(typename);
        if (queryResult!=null){
            System.out.println("增加类别失败，已存在此"+typename+"类别");
            throw new RuntimeException();
        }
        PetType petType=new PetType();
        petType.setTypeName(typename);
        petType.setIsDelete(petTypeDTO.getIsDelete());
        LocalDateTime now=LocalDateTime.now();
        petType.setGmtCreate(now);
        petType.setGmtModified(now);
        int row=petTypeMapper.addType(petType);
        if (row!=1){
            System.out.println("加入错误，服务器忙");
            throw new RuntimeException();
        }
    }

    @Override
    public List<PetTypeVO> getPetTypeName() {
        log.warn("petTypeMapper.getPetTypeName():{}",petTypeMapper.getPetTypeName());
        return petTypeMapper.getPetTypeName();
    }


    @Override
    public void updatePetType(String typeName,String newName ) {
        PetType queryResult=petTypeMapper.getByTypeName(typeName);
        if (queryResult==null){
            System.out.println("修改类别失败，此类别不存在");
            throw new RuntimeException();
        }

        PetType petType=new PetType();
        petType.setTypeName(newName);
        petTypeMapper.updatePetType(typeName,newName);
        System.out.println("修改完成");

    }

    @Override
    public void updateIsDelete(String typeName, Integer isDelete) {
        PetType isExist= petTypeMapper.getByTypeName(typeName);
        System.out.println("获取的名字为："+typeName);
        System.out.println(isExist);
        if (isExist==null){
            System.out.println("此宠物类型不存在");
            throw new RuntimeException("宠物类型输入错误");
        }
        petTypeMapper.updateIsDelete(typeName,isDelete);
        System.out.println("修改完成！");
    }
}
